package com.hnevc.musicplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity  {

    private static SeekBar musicSeekBar;
    private static TextView progress_tv;
    private static TextView total_tv;
    private Button play_btn;
    private Button pause_btn;
    private Button continue_btn;
    private Button exit_btn;
    private MusicService.MusicControl musicControl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initDate();
        initEvent();
    }

    private void initView(){
        musicSeekBar = findViewById(R.id.music_seekbar);
        progress_tv = findViewById(R.id.progress_tv);
        total_tv = findViewById(R.id.total_tv);
        play_btn = findViewById(R.id.play_btn);
        pause_btn = findViewById(R.id.pause_btn);
        continue_btn = findViewById(R.id.continue_btn);
        exit_btn = findViewById(R.id.exit_btn);

    }
    private void initDate(){
            Intent intent = new Intent(MainActivity.this,MusicService.class);
            MyServiceConn conn = new MyServiceConn();//创建链接服务
            bindService(intent,conn,BIND_AUTO_CREATE);//第一个告诉绑定服务工具 需要绑定的服务 第二个 创建链接服务 第三个 绑定后自动创建
    }
    private void initEvent(){
        play_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            musicControl.play();
            }
        });
    musicSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        //进度条发生改变时
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }
        //进度条滑动时
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }
        //进度条停止滑动
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            int progress = seekBar.getProgress();
            musicControl.seekTo(progress);
        }
    });
    }

//    @Override
 //   public void onClick(View v) {
  //      switch (v.getId()){
  //          case R.id.play_btn:
  //              musicControl.play();
  //              break;
   //         case R.id.pause_btn:
 //               break;
  //          case R.id.continue_btn:
  //              break;
  //          case R.id.exit_btn:
  //              break;
 //       }
 //   }
public static Handler handler = new Handler(){
    @Override
    public void handleMessage(@NonNull Message msg) {
        Bundle bundle =msg.getData();//获取服务传递过来的数据（子线程传递的数据）
        int duration = bundle.getInt("duration");//总时长
        int currentPosition = bundle.getInt("currentPosition");//播放进度
        musicSeekBar.setMax(duration);//设置当前进度条的总长度
        musicSeekBar.setProgress(currentPosition);//当前播放进度
        int minute = duration / 1000 / 60;
        int second = duration / 1000 % 60;
        String strMinute = null;
        String strSecond = null;
        if (minute < 10){
            strMinute = "0"+minute;
        }else {
            strMinute = minute + "";
        }
        if (second < 10){
            strSecond = "0"+second;
        }else {
            strSecond = second + "";
        }
        total_tv.setText(strMinute+":"+strMinute);
        //当前播放时间
        minute = currentPosition / 1000 / 60;
        second = currentPosition /1000 % 60;
        if (minute<10){
            strMinute="0"+minute;
        }else {
            strMinute = minute +"";
        }
        if (second<10){
            strSecond = "0"+second;
        }else {
            strSecond =second +"";
        }
        progress_tv.setText(strMinute+":"+strSecond);
    }
};
    class  MyServiceConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicControl = (MusicService.MusicControl) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}